package Controller.Commodit;

import Models.dao.CartDao;
import Models.enity.AdminUser;
import Models.dao.CommoditDao;
import Models.enity.Cart;
import Models.enity.Commodit;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/transactional/addcart")
public class addCart extends HttpServlet{

    private CartDao cartDao = new CartDao();
    private CommoditDao commoditDao = new CommoditDao();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String quality = req.getParameter("quality");
        if (Integer.parseInt(quality) <0) resp.sendRedirect("/transactional/orderRequest");


        Cart cart = cartDao.findByCommoitId(id);
        AdminUser user =(AdminUser) req.getSession().getAttribute("user");
        String user_id = user.getId().toString();
        Commodit commodit = commoditDao.findById(id);
        System.out.println(cart);
        int tmp;
        if (cart != null)
            tmp = cartDao.addQuality(id,Integer.parseInt(cart.getQuality()) + Integer.parseInt(quality));
        else
            tmp = cartDao.insert(user_id,id,commodit.getPrice(),commodit.getLimit_price(),quality);

        if (tmp != -1) resp.sendRedirect("/transactional/orderRequest?success=1");
    }
}
